package com.yanqu.road.server.manager.activity.feast.task;

import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.manager.activity.feast.CrossFeastMgr;
import com.yanqu.road.server.manager.activity.feast.activity.CrossFeastActivity;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

public class CrossFeastPvpNpcTask extends GeneralTask {

    private long npcMoveTaskTime;

    private long npcActionTaskTime;

    @Override
    public void doRun() {

        // 移动 次/0.1s
        if (System.currentTimeMillis() > npcMoveTaskTime) {
            npcMoveTaskTime = System.currentTimeMillis() + 100;
            for (CrossFeastActivity activity : CrossFeastMgr.getActivityMap().values()) {
                try {
                    if (!ActivityHelper.activityInTime(activity.getConfig().getActivityInfo())) {
                        continue;
                    }
                    activity.pvpNpcMoveTask();
                } catch (Exception e) {
                    getLOGGER().error("error : ", e);
                }
            }

        }

        // npc行为 次/1s
        if (System.currentTimeMillis() > npcActionTaskTime) {
            npcActionTaskTime = System.currentTimeMillis() + 1000;
            for (CrossFeastActivity activity : CrossFeastMgr.getActivityMap().values()) {
                try {
                    if (!ActivityHelper.activityInTime(activity.getConfig().getActivityInfo())) {
                        continue;
                    }
                    activity.pvpNpcActionTask();
                } catch (Exception e) {
                    getLOGGER().error("error : ", e);
                }
            }
        }

        //移动同步
        Map<Integer, CrossFeastActivity> activityMap = CrossFeastMgr.getActivityMap();
        if (activityMap == null || activityMap.isEmpty()) {
            // 无活动
            return;
        }
        for (CrossFeastActivity feastActivity : activityMap.values()) {
            try {
                if (!ActivityHelper.activityInTime(feastActivity.getConfig().getActivityInfo())) {
                    continue;
                }
                feastActivity.syncPositionTask();
            } catch (Exception e) {
                getLOGGER().error("syncPositionTask error : ", e);
            }
        }
    }
}
